home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
PROGNG_C
/
TURBOCU2.LZH
/
RMTREE.C
< prev
next >
Wrap
Text File
|
1987-09-05
|
3KB
|
153 lines
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <conio.h>
#include <dir.h>
#include <dos.h>
#include <io.h>
main(argc, argv)
int argc;
char *argv[];
{
if (argc != 2) {
puts("usage: rmtree directory_name to remove a directory tree recursively.");
puts("\nchdir to the parent directory of directory_name to use rmtree.");
puts("rmtree will delete *all* files and directories including directory_name.");
puts("hidden, system and r/o files will be deleted after confirmation.");
puts("\nTHIS PROGRAM SHOULD BE USED WITH CAUTION!\007\n");
_exit();
}
if ((pos('?',argv[1]) > -1) || (pos('*',argv[1]) > -1)) {
puts("Wildcards not allowed.");
_exit();
}
printf("\nAbout to remove tree %s. Are you sure? ", argv[1]);
if (getconfirm())
scandir(argv[1]);
else puts("\nAborted.");
}
/*
** scan a directory
*/
scandir(filemask)
char *filemask;
{
int done;
struct ffblk ffblk;
char dirname[80], localmask[80];
done = findfirst(filemask, &ffblk, 0x3f);
if (done)
perror("rmtree");
while (!done) {
if (ffblk.ff_attrib == 16) { /* we found another directory */
if (ffblk.ff_name[0] != '.') { /* spare this one and parent */
strcpy(dirname, ffblk.ff_name);
if (chdir(dirname)) { /* try to chdir for recursion */
printf("rmtree: can't chdir to %s.\n", dirname);
_exit();
};
strcpy(localmask, "*.*"); /* now get *all* files and .. */
scandir(localmask); /* play it again, sam! */
chdir(".."); /* back from recursion, now ..*/
if (rmdir(dirname)) { /* we remove the emptied dir */
perror("rmtree");
_exit();
}
}
}
else { /* here we got a normal file, so we just unlink it */
do_delete(ffblk.ff_name, ffblk.ff_attrib);
}
done = findnext(&ffblk); /* get next dir-entry til done */
}
}
/*
** delete a file, give user a chance to abort
*/
do_delete(fname, fattr)
char *fname;
int fattr;
{
if (fattr & FA_RDONLY) {
printf("%s read only. continue? ", fname);
if (!getconfirm())
_exit();
else {
_chmod(fname, 1, 0);
fattr = 0;
}
}
if (fattr & FA_HIDDEN) {
printf("%s hidden file. continue? ", fname);
if (!getconfirm())
_exit();
else {
_chmod(fname, 1, 0);
fattr = 0;
}
}
if (fattr & FA_SYSTEM) {
printf("%s system file. continue? ", fname);
if (!getconfirm())
_exit();
else {
_chmod(fname, 1, 0);
fattr = 0;
}
}
unlink(fname);
}
/*
** get user's confirmation for anything
*/
int getconfirm()
{
char ch;
do {
ch = toupper(getch());
if ((ch == '\x1b') || (ch == '\x03')) {
puts("\nAborted.");
_exit();
}
} while ((ch != 'N') && (ch != 'Y'));
putchar(ch);
putchar('\n');
return(ch == 'Y');
}
/*
* find a given character in a given string *************************
*/
int pos (ch,string)
char ch;
char string[];
{
int i;
for (i=0;string[i] != '\0'; i++)
if (string[i] == ch)
return(i);
return (-1);
}